package com.one.blocks.rbac.convert;

import com.one.blocks.mvc.convert.BaseDomainConvert;
import com.one.blocks.rbac.controller.query.SysMenuQuery;
import com.one.blocks.rbac.controller.vo.SysMenuGrantVo;
import com.one.blocks.rbac.controller.vo.SysMenuTreeVo;
import com.one.blocks.rbac.controller.vo.SysMenuVo;
import com.one.blocks.rbac.domain.SysMenu;
import com.one.blocks.rbac.domain.SysRoleMenu;
import com.one.blocks.rbac.util.SubjectHelper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-02-14 13:57:56
 */
@Mapper(uses = {TimestampConvert.class}, imports = {SubjectHelper.class})
public interface SysMenuConvert extends BaseDomainConvert<SysMenu, SysMenuVo, SysMenuQuery> {

    SysMenuConvert INSTANCE = Mappers.getMapper(SysMenuConvert.class);

    SysMenuTreeVo entityToTreeVo(SysMenu sysMenu);

    List<SysMenuTreeVo> entityToTreeVoList(List<SysMenu> sysMenus);

//    SysMenu treeVoToEntity(SysMenuTreeVo sysMenuTreeVo);

    default List<SysRoleMenu> grantVoToGrantEntity(SysMenuGrantVo sysRoleGrantVo) {
        if (CollectionUtils.isEmpty(sysRoleGrantVo.getMenuIds())) {
            return null;
        }
        List<SysRoleMenu> dataToInsert = new ArrayList<>(sysRoleGrantVo.getMenuIds().size());
        for (Integer menuId : sysRoleGrantVo.getMenuIds()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();

            sysRoleMenu.setRoleId(sysRoleGrantVo.getRoleId());
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setCreateId(SubjectHelper.getSubjectId());
            sysRoleMenu.setCreateTime(SubjectHelper.getAccessTime());

            dataToInsert.add(sysRoleMenu);
        }
        return dataToInsert;
    }
}